<?php

error_reporting('E_ALL & ~E_NOTICE');
/**
 *
 * @copyright 2011 by Dan, Steven, Brice, Mike, and Aaron.  This program is part
 * of Internship 2 Career, which is free software. It comes with absolutely no
 * warranty. You can redistribute or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation (For
 * more information, see <http://www.gnu.org/licenses/).
 *
 * @version March 6, 2011
 *
 * @author Aaaron Jarzombek
 */
include('functions.php');
include('../dbOrganization.php');
include('../../model/Organization.php');
include('../dbCategory.php');
include('../../model/Category.php');
include('../dbContact.php');
include('../../model/Contact.php');
include('../dbStudent.php');
include('../../model/Student.php');
include('../dbOpportunity.php');
include('../../model/Opportunity.php');
include('../dbMajor.php');
include('../../model/Major.php');
include('../dbInternship.php');
include('../../model/Internship.php');
include('../dbUser.php');
include('../../model/User.php');

echo ("Initializing Data...<br />");

//one call does all he work of populating the database with sample data
populateDatabase();

/**
 * Populates the database with sample data. Makes calls to insertXXXData()
 * functions in this file, where XXX corresponds to a table in the database.
 * Each of these calls takes as arguments the data that's inserted.
 */
function populateDatabase() {
//Category
    $id = 0;
    insertCategoryData('Advertising/Graphic Design', $id);
    verifyInsertedCategory($id);

    $id = 0;
    insertCategoryData('CIS Related', $id);
    verifyInsertedCategory($id);

    $id = 0;
    insertCategoryData('Communications', $id);
    verifyInsertedCategory($id);

    $id = 0;
    insertCategoryData('Community Service', $id);
    verifyInsertedCategory($id);

    $id = 0;
    insertCategoryData('Educational', $id);
    verifyInsertedCategory($id);

    $id = 0;
    insertCategoryData('Event Planning', $id);
    verifyInsertedCategory($id);

    $id = 0;
    insertCategoryData('Finance/Economics', $id);
    verifyInsertedCategory($id);

    $id = 0;
    insertCategoryData('Government/Political', $id);
    verifyInsertedCategory($id);

    $id = 0;
    insertCategoryData('Health & Human Services', $id);
    verifyInsertedCategory($id);

    $id = 0;
    insertCategoryData('Legal', $id);
    verifyInsertedCategory($id);

    $id = 0;
    insertCategoryData('Marketing/PR', $id);
    verifyInsertedCategory($id);

    $id = 0;
    insertCategoryData('Psychology', $id);
    verifyInsertedCategory($id);

    $id = 0;
    insertCategoryData('Research', $id);
    verifyInsertedCategory($id);

    $id = 0;
    insertCategoryData('Sales', $id);
    verifyInsertedCategory($id);

    $id = 0;
    insertCategoryData('Technology', $id);
    verifyInsertedCategory($id);

    $id = 0;
    insertCategoryData('Various', $id);
    verifyInsertedCategory($id);

    $id = 0;
    insertCategoryData('Web/Information Technology', $id);
    verifyInsertedCategory($id);

    $id = 0;
    insertCategoryData('Writing/Journalism', $id);
    verifyInsertedCategory($id);

//ORGANIZATION
    //HAM (1)
    $id = 0;
    insertOrganizationData("Ham", "blue", "123 Cat Dr.", "Suite #5",
            "Manchester", "NH", "03031", $id);
    verifyInsertedOrganization($id);

    //PUP N SUDS (2)
    insertOrganizationData("Pup N Suds", "They sponsored BRINK!", "123 Main Street", null, "Concord", "NH", "03031", $id);
    verifyInsertedOrganization($id);

    ////BOSTON BRUINS (3)
    insertOrganizationData("Boston Bruins", "Only the best hockey team ever!!!", "100 Legends Way", null, "Boston", "MA", "02114", $id);
    verifyInsertedOrganization($id);

    ////MICROSOFT (4)
    insertOrganizationData("Microsoft", "Do stuff", "6 Main Street", null,
            "Nashua", "New Hampshire", "12134", $id);
    verifyInsertedOrganization($id);

    ////APPLE (5)
    insertOrganizationData("Apple", "Do stuff with computers that look like an apple", "48329 Brookview Drive", null, "Londonderry",
            "NH", "95014", $id);
    verifyInsertedOrganization($id);

    ////UNH (6)
    insertOrganizationData("University of New Hampshire", "School in the middle of no where", "340 Dame Road", null, "Durham", "NH",
            "03824", $id);
    verifyInsertedOrganization($id);

    ////WALMART (7)
    insertOrganizationData("Wal-mart", "Destroyed the economy and made China huge", "83 Long Way", null, "Salem", "NH", "39205", $id);
    verifyInsertedOrganization($id);

    ////FACEBOOK (8)
    insertOrganizationData("Facebook", "Social Network", "2383 Main Street",
            null, "Dracut", "MA", "39205", $id);
    verifyInsertedOrganization($id);

    ////BAIN (9)
    insertOrganizationData("Bain", "Bain and Company is a global management consulting firm.", "131 Dartmouth Street", null, "Boston", "MA",
            "02199", $id);
    verifyInsertedOrganization($id);

    ////PARKLAND MEDICAL (10)
    insertOrganizationData("Parkland Medical Center", "Hospital", "1 Parkland Drive", null, "Derry", "NH", "03038", $id);
    verifyInsertedOrganization($id);

//CONTACT
    $id = 0;
    insertContactData(1, "Smith", "Ben", "BSmith@aol.com", "999-999-9999",
                    "111", "111-111-1111", $id);
    verifyInsertedContact($id);
    
    insertContactData(1, "Sara", "McAwesome", "BSmith@aol.com", "999-999-9999",
                    "111", "111-111-1111", $id);
    verifyInsertedContact($id);
    
    insertContactData(1, "Harry", "Frankenstein", "BSmith@aol.com", "999-999-9999",
                    "111", "111-111-1111", $id);
    verifyInsertedContact($id);

    insertContactData(2, "Happy", "Gilmore", null, null,
                    null, null, $id);
    verifyInsertedContact($id);

    insertContactData(3, "Patrice", "Bergeron", null, null,
                    null, null, $id);
    verifyInsertedContact($id);
    insertContactData(4, "Daniel", "Pepin", "BSmith@aol.com", "999-999-9999",
                    "111", "111-111-1111", $id);
    verifyInsertedContact($id);

    insertContactData(5, "John", "Smith", null, null,
                    null, null, $id);
    verifyInsertedContact($id);

    insertContactData(6, "Jeremy", "Something", null, null,
                    null, null, $id);
    verifyInsertedContact($id);
    insertContactData(7, "David", "Something", "BSmith@aol.com", "999-999-9999",
                    "111", "111-111-1111", $id);
    verifyInsertedContact($id);

    insertContactData(8, "William", "Hung", null, null,
                    null, null, $id);
    verifyInsertedContact($id);

    insertContactData(9, "Thomas", "Rice", null, null,
                    null, null, $id);
    verifyInsertedContact($id);
    
    insertContactData(10, "Smitty", "Wergeryegermanjenson", null, null,
                    null, null, $id);
    verifyInsertedContact($id);
    

   //STUDENT
    $id = 0;
    insertStudentData("Jeff", "CarMichael", "Jcar", 3.12, "ENG", "Senior",
                    "2011", "Yes", $id);
    verifyInsertedStudent($id);

    insertStudentData("Bob", "Saget", "bsaget", 3.19, "ENG", "Super Senior",
                    "2011", "Yes", $id);
    verifyInsertedStudent($id);

    insertStudentData("Tyler", "Seguin", "tseguin", 4.00, "CIS", "Freshmen",
                    "2015", "Yes", $id);
    verifyInsertedStudent($id);

//OPPORTUNITY
    $id = 0;
    insertOpportunityData(1, 1, "Programmer", "3", "2011 Spring", "500/week",
                    "5 Weeks", "Design", "C++", "Go Left", "No Comment",
                    "CIS Related Communications", 1, $id);
    verifyInsertedOpportunity($id);

    insertOpportunityData(1, 2, "System Administrator", "2", "2011 Fall", "15/hour",
                    "6 Months", "doing stuff", null, null, null, "CIS Related", 
                    1, $id);
    verifyInsertedOpportunity($id);

    insertOpportunityData(1, 3, "Network Adminstrator Assistant", "5", "2011 Spring
    /Fall", "40,000/year", "One year", "Doing networking stuff", null, null, null,
                    "CIS Related", 1, $id);
    verifyInsertedOpportunity($id);

    insertOpportunityData(2, 4, "Dog Washer", "4", "2011 Summer", "10/hour", "Summer",
                    "Washing", null, null, null, "Marketing/PR", 0, $id);
    verifyInsertedOpportunity($id);

    insertOpportunityData(2, 4, "Dog Walker", "22", "2011 Summer", "3,000/summer",
                    "Every summer", "Walking", null, null, null, "Communications
                    Government/Political", 0, $id);
    verifyInsertedOpportunity($id);

    insertOpportunityData(2, 4, "Cashier", "2", "2011 Summer", "10/hour", "Summmer",
                    "Ringing people out", null, null, null, "Finance/Economics", 
                     0, $id);
    verifyInsertedOpportunity($id);

    insertOpportunityData(3, 5, "Zamboni Driver", "2", "2011 Fall", "100/hour", "One
    semester", "Driving", null, null, null, "Health & Human Services", 0, $id);
    verifyInsertedOpportunity($id);

    insertOpportunityData(3, 5, "Referee", "4", "2011 Fall", "250/week", "16 weeks",
                    "Ice Hockey", null, null, null, "Educational Various", 0, $id);
    verifyInsertedOpportunity($id);

    insertOpportunityData(3, 5, "Ultimate Fan", "1", "2011 Fall/2012 Spring",
                    "1,000,000/season", "Every season", "Ice Hockey", null, null,
                    null, "Event Planning Communications Marketing/PR", 0, $id);
    verifyInsertedOpportunity($id);

    insertOpportunityData(4, 6, "Programmer", "4", "2011 Fall", "650/week", "16 weeks",
                    "Make programs", null, null, null, "CIS Related Technology", 
                    0, $id);
    verifyInsertedOpportunity($id);

    insertOpportunityData(5, 7, "Programmer", "4", "2011 Fall", "550/week", "16 weeks",
                    "Make applications", null, null, null, "CIS Related Technology",
                    0, $id);
    verifyInsertedOpportunity($id);

    insertOpportunityData(6, 8, "Researcher", "4", "2011 Fall", "1,500/semester",
                    "16 weeks", "Research things for professors", null, null, null,
                    "Educational Research", 0, $id);
    verifyInsertedOpportunity($id);

    insertOpportunityData(7,9, "Promotional Event Planner", "4", "2011 Fall",
                    "12/hour", "16 weeks", "Ruining the country", null, null, null,
                    "Event Planning", 0, $id);
    verifyInsertedOpportunity($id);

    insertOpportunityData(8, 10, "Public Relations Officer", "4", "2011 Spring",
                    "20/hour", "12 weeks", "Help getting new facebook stuff out there",
                    null, null, null, "Event Planning Marketing/PR Sales", 0, $id);
    verifyInsertedOpportunity($id);

    insertOpportunityData(9, 11, "Consultant", "4", "2011 Fall", "400/week", "Two
                 projects", "Help consultanting companies", null, null, null,
                    "Finance/Economics", 0, $id);
    verifyInsertedOpportunity($id);

    insertOpportunityData(10, 12, "Assistant to Legal", "4", "2012 Spring", "12/hour",
                    "16 weeks", "Legal matters", null, null, null, "Legal", 0, $id);
    verifyInsertedOpportunity($id);
    

    


//INTERNSHIP
    $id = 0;
    insertInternshipData(1, 1, "35", "special", "2011 Spring", $id);
    verifyInsertedInternship($id);

    insertInternshipData(1, 2, "30", "special", "Fall");
    verifyInsertedInternship($id);

    insertInternshipData(1, 3, "25", "special", "Summer");
    verifyInsertedInternship($id);

//USER
    $id = 0;
    insertUserData("admin", "kakIDMpFUb/KaF210KppTalJBcXUSb6", "Admin", "", "3", $id);
    verifyInsertedUser($id);

    insertUserData("student", "yO4y/YwffHx1cSctK4YJzcUUn7qSNzq", "Student", "", "1", $id);
    verifyInsertedUser($id);

    insertUserData("manager", "GE8Uuwx4VicH2S", "Kelly", "Powers", "2", $id);
    verifyInsertedUser($id);
}

//end of populateDatabase() function

function insertOrganizationData($name, $description, $address, $address1, $city, $state, $zip, &$id) {
    $organization = new Organization($name, $description, $address, $address1,
                    $city, $state, $zip);

    $result = dbOrganization::addOrganization($organization, $id);
    if ($result == false) {
        die("Organization could not be updated with specified information.<br />");
    }
}

/**
 * Outputs data that was inserted in the organization table by querying the table.
 * Either an error message is printed out about none or multiple instances
 * were found for the given primary key value; or the attribute values of the
 * found instance are printed out
 * @param integer $id  The primary key value based on which the query is done.
 */
function verifyInsertedOrganization($id) {
    $output = "";
    $error = dbOrganization::getAllByID($id, $output);
    if ($error == true) {
        printf("%s<br />", $output);
    } else {
        $o = new Organization($output['organizationName'], $output['description'],
                        $output['address'], $output['address1'], $output['city'],
                        $output['stateName'], $output['zip']);
        printf("Inserted: Organization with ID=%d has %s.<br />", $id,
                $o->printAllAttributes());
    }
}

function insertStudentData($fname, $lname, $username, $currentGPA, $major, 
                            $yearStatus, $graduationYear, $gotHired, &$id) {
    $s = new Student($fname, $lname, $username, $currentGPA, 
            $major, $yearStatus, $graduationYear, $gotHired);

    $result = dbStudent::addStudent($s, $id);
    printf("->StudentID: %d<br />", $id);
    if ($result == false) {
        die("Student could not be updated with specified information.<br />");
    }
}

function verifyInsertedStudent($id) {
    $output = "";
    $error = dbStudent::getAllByID($id, $output);
    if ($error == true) {
        printf("%s<br />", $output);
    } else {
        $s = new Student($output['fname'], $output['lname'], $output['username'],
                $output['currentGPA'], $output['major'], $output['yearStatus'],
                $output['graduationYear'], $output['gotHired']);
        printf("Inserted: Student with ID=%d has %s.<br />", $id,
                $s->printAllAttributes());
    }
}

function insertContactData($organizationID, $fname, $lname, $email, $phone, 
                            $ext, $fax, &$id) {
    $contact = new Contact($organizationID, $fname, $lname, $email, $phone,
                            $ext, $fax);

    $result = dbContact::addContact($contact, $id);
    if ($result == false) {
        die("Contact could not be updated with specified information.<br />");
    }
}

function verifyInsertedContact($id) {
    $output = "";
    $error = dbContact::getAllByID($id, $output);
    if ($error == true) {
        printf("%s<br />", $output);
    } else {
        $c = new Contact($output['organizationID'],
                        $output['fname'], $output['lname'], $output['email'],
                        $output['phone'], $output['ext'], $output['fax']);
        printf("Inserted: Contact with ID=%d has %s.<br />", $id,
                $c->printAllAttributes());
    }
}

function insertCategoryData($categoryName, &$id) {
    $category = new Category($categoryName);

    $result = dbCategory::addCategory($category, $id);
    if ($result == false) {
        die("Category could not be updated with specified information.<br />");
    }
}

function verifyInsertedCategory($id) {
    $output = "";
    $error = dbCategory::getAllByID($id, $output);
    if ($error == true) {
        printf("%s<br />", $output);
    } else {
        $c = new Category($output['categoryName']);
        //printf("->in verifyInsertedCategory %s", $output['categoryName']);
        printf("Inserted: Category with ID=%d has %s.<br />", $id,
                $c->printAllAttributes());
    }
}

function insertOpportunityData($organizationID, $contactID, $title,
                            $numPositionsAvailable, $semester, $payRate,
                            $duration, $responsibilities,
                            $qualifications, $instructions, $comments,
                            $categories, $pending, $dateAdded, &$id) {
    $opportunity = new Opportunity($organizationID, $contactID, $title,
                            $numPositionsAvailable, $semester, $payRate,
                            $duration, $responsibilities,
                            $qualifications, $instructions, $comments,
                            $categories, $pending, $dateAdded);

    $result = dbOpportunity::addOpportunity($opportunity, $id);
    if ($result == false) {
        die("Opportunity could not be updated with specified information.<br />");
    }
}

function verifyInsertedOpportunity($id) {
    $output = "";
    $error = dbOpportunity::getAllByID($id, $output);
    if ($error == true) {
        printf("%s<br />", $output);
    } else {
        $opportunity = new Opportunity($output['organizationID'], $output['contactID'],
                $output['title'], $output['numPositionsAvailable'],
                $output['semester'], $output['payRate'], $output['duration'],
                $output['responsibilities'], $output['qualifications'],
                $output['instructions'], $output['comments'],
                $output['categories'], $output['pending'], $output['dataAdded']);
        printf("Inserted: Opportunity with ID=%d has %s.<br />", $id,
                $opportunity->printAllAttributes());
    }
}

function insertInternshipData($opportunityID, $studentID, $hours, $special,
                              $semester, &$id) {
    $internship = new Internship($opportunityID, $studentID, $hours, $special,
                              $semester);

    $result = dbInternship::addInternship($internship, $id);
    if ($result == false) {
        die("Internship could not be updated with specified information.<br />");
    }
}

function verifyInsertedInternship($id) {
    $output = "";
    $error = dbInternship::getAllByID($id, $output);
    if ($error == true) {
        printf("%s<br />", $output);
    } else {
        $internship = new Internship($output['opportunityID'],
                $output['studentID'], $output['hours'], $output['special'],
                $output['semester']);
        //printf("->in verifyInsertedInternship %s", $output['opportunityID']);
        printf("Inserted: Internship with ID=%d has %s.<br />", $id,
                $internship->printAllAttributes());
    }
}

function insertUserData($username, $password, $fname, $lname,
                              $permissionLevel, &$id) {
    $user = new User($username, $password, $fname, $lname,
                              $permissionLevel);

    $result = dbUser::addUser($user, $id);
    if ($result == false) {
        die("User could not be updated with specified information.<br />");
    }
}

function verifyInsertedUser($id) {
    $output = "";
    $error = dbUser::getAllByID($id, $output);
    if ($error == true) {
        printf("%s<br />", $output);
    } else {
        $user = new User($output['username'],
                $output['password'], $output['fname'], $output['lname'],
                $output['permissionLevel']);
        printf("Inserted: User with ID=%d has %s.<br />", $id,
                $user->printAllAttributes());
    }
}

?>
